//
//  WPDIYSegment.m
//  WPDIYSegment
//
//  Created by WeiPan on 16/4/4.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPDIYSegment.h"
#define DEFAULTFONT  [UIFont systemFontOfSize:14]
#define DEFAULTSELECTCOLOR  [UIColor redColor]
#define DEFAULTCOLOR  [UIColor blackColor]
@interface WPDIYSegment ()
@property (nonatomic, strong) NSArray * titles;
@property (nonatomic, strong) UIView * lineView;
@property (nonatomic, strong) UIButton *selectedBtn;
- (void)initializeAppearance;/**< 一般界面初始化 */


@end
@implementation WPDIYSegment

#pragma mark - init
- (instancetype)initWithTitles:(NSArray *)titles andNormalColor:(UIColor *)color andSelectedColor:(UIColor *)selectedColor andFont:(UIFont *)font {
    self = [super init];
    if (self) {
        self.selectedIndex = 0;
        self.titles = titles;
        self.titleColor = color;
        self.titleSelectColor = selectedColor;
        self.titleFont = font;
        [self initializeAppearance];
    }
    return self;
}
- (instancetype)initWithTitles:(NSArray *)titles andNormalColor:(UIColor *)color andSelectedColor:(UIColor *)selectedColor {
    self = [self initWithTitles:titles andNormalColor:color andSelectedColor:selectedColor andFont:DEFAULTFONT];
    return self;
}
- (instancetype)initWithTitles:(NSArray *)titles andNormalColor:(UIColor *)color {
    return [self initWithTitles:titles andNormalColor:color andSelectedColor:DEFAULTSELECTCOLOR];
}
- (instancetype)initWithTitles:(NSArray *)titles {
    return [self initWithTitles:titles andNormalColor:DEFAULTCOLOR];
}

- (WPDIYSegment *)setSegmentOriginPoint:(CGPoint)point{
    CGRect frame = self.frame;
    frame.origin.x = point.x;
    frame.origin.y = point.y;
    self.frame = frame;
    return self;
}
- (void)initializeAppearance {
    self.frame = CGRectMake(9, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 30*DHFlexibleHorizontalMutiplier());
    self.backgroundColor = COLOR_RGB(241, 241, 241, 1);
    self.layer.borderWidth = 0.6;
    self.layer.borderColor = [UIColor colorWithRed:0.698f green:0.698f blue:0.698f alpha:1.00f].CGColor;
    [self setData:self.titles NormalColor:self.titleColor SelectColor:self.titleSelectColor Font:self.titleFont];
}
- (void)setData:(NSArray *)titles NormalColor:(UIColor *)n_color SelectColor:(UIColor *)s_color Font:(UIFont *)font{
    
    self.titles = titles;
    CGFloat _width = [UIScreen mainScreen].bounds.size.width / titles.count;
    
    for (int i=0; i<titles.count; i++) {
        UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
        item.frame = CGRectMake(i * _width, 0, _width, CGRectGetHeight(self.frame));
        [item setTitle:titles[i] forState:UIControlStateNormal];
        [item setTitleColor:n_color forState:UIControlStateNormal];
        [item setTitleColor:s_color forState:UIControlStateSelected];
        item.titleLabel.font = font;
        item.tag = i + 10;
        [item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:item];
        
        if (i == 0) {
            
            item.selected = YES;
            self.selectedBtn = item;
            
            self.lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 1.5, _width, 1.5)];
            self.lineView.layer.cornerRadius = 3.0;
            self.lineView.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            self.lineView.layer.masksToBounds = YES;
            [self addSubview:self.lineView];
        }
    }
}
- (void)clickItem:(UIButton *)button{
    
    [self setAnimation:button.tag-10];
    if (self.indexBlock != nil) {
        self.selectedIndex = button.tag - 10;
        self.indexBlock( button.tag-10, self.titles[button.tag-10]);
    }
    if (self.delegate && [_delegate respondsToSelector:@selector(WPDIYSegmentClicked:)]) {
        [_delegate WPDIYSegmentClicked:button.tag - 10];
    }
}

/***********************【属性】***********************/

- (void)setLineColor:(UIColor *)lineColor{
    self.lineView.backgroundColor = lineColor;
}

- (void)setLineHeight:(CGFloat)lineHeight{
    CGRect frame = self.lineView.frame;
    frame.size.height = lineHeight;
    frame.origin.y = CGRectGetHeight(self.frame) - lineHeight - 1;
    self.lineView.frame = frame;
}

- (void)setLineCornerRadius:(CGFloat)lineCornerRadius{
    self.lineView.layer.cornerRadius = lineCornerRadius;
}
/***********************【回调】***********************/
- (void)getCurrentIndexWithBlock:(void (^)(NSInteger, NSString *))indexBlock {
    self.indexBlock = indexBlock;
}
- (void)selectIndexWithIndex:(NSInteger)index {
    if (index < 0) {
        index = 0;
    }
    index = index > _titles.count - 1 ? _titles.count - 1:index;
    [self setAnimation:index];
}
- (void)setViewIndex:(NSInteger)index{
    if (index < 0) {
        index = 0;
    }
    
    if (index > self.titles.count - 1) {
        index = self.titles.count - 1;
    }
    
    [self setAnimation:index];
}

/***********************【其他】***********************/

- (void)setAnimation:(NSInteger)index{
    
    UIButton *tempBtn = (UIButton *)[self viewWithTag:index+10];
    self.selectedBtn.selected = NO;
    tempBtn.selected = YES;
    self.selectedBtn = tempBtn;
    
    CGFloat x = index * ([UIScreen mainScreen].bounds.size.width / _titles.count);
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.lineView.frame;
        frame.origin.x = x;
        self.lineView.frame = frame;
    }];
}

@end
